using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace GSN
{
    public partial class MessageBoxTimeout : Form
    {
        private string _text = null;
        private string _caption = null;
        private int _timeout = -1;
        private int secondsCount = 0;

        public MessageBoxTimeout(string text, string caption, int timeout)
        {
            InitializeComponent();
            _text = text;
            _caption = caption;
            _timeout = timeout;
        }

        private void MessageBoxTimeout_Load(object sender, EventArgs e)
        {
            
            pictureBox1.Image = SystemIcons.Information.ToBitmap();

            txtText.Text = _text;
            txtText.Focus();

            this.Text = _caption;
            btnOk.Text = string.Format("OK ({0})", _timeout);

            // make form symmetric
            this.Width = txtText.Width + pictureBox1.Width + 50;
            btnOk.Left = (this.Width - btnOk.Width) / 2;
            
            timer1.Interval = 1000; // second
            timer1.Start();
        }

        public static void Show(string text, string caption, int timeout)
        {
            MessageBoxTimeout f = new MessageBoxTimeout(text, caption, timeout);
            f.ShowDialog();
            f = null;
        }

        private void timer1_Tick(object sender, EventArgs e)
        {
            secondsCount++;
            btnOk.Text = string.Format("OK ({0})", _timeout - secondsCount);
            if (secondsCount == _timeout)
            {
                btnOk_Click(sender, e);
            }
        }

        private void btnOk_Click(object sender, EventArgs e)
        {
            timer1.Stop();
            this.DialogResult = DialogResult.OK;
            this.Close();
        }
    }
}